package io.renren.modules.pms.controller;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.renren.modules.pms.entity.dto.PmsProductCategoryDTO;
import io.renren.modules.pms.entity.dto.ProductWithChildrenDTO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import io.renren.modules.pms.entity.PmsProductCategoryEntity;
import io.renren.modules.pms.service.PmsProductCategoryService;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.R;

import javax.servlet.http.HttpServletRequest;
import javax.websocket.server.PathParam;
import javax.xml.crypto.Data;


/**
 * 产品分类
 *
 * @author LuoShun
 * @email lori6166@163.com
 * @date 2021-11-12 15:19:30
 */
@Api(tags = "商品分类管理")
@RestController
@RequestMapping("/pms/productCategory")
public class PmsProductCategoryController {
    @Autowired
    private PmsProductCategoryService pmsProductCategoryService;

    /**
     * 获取商品分类列表
     */
    @ApiOperation("获取商品分类列表")
    @GetMapping("/list")
    @RequiresPermissions("pms:pmsproductcategory:list")
    public R list(@RequestParam Map<String, Object> params){
//        System.out.println(params.toString());
//        return R.ok();
        PageUtils page = pmsProductCategoryService.queryPage(params);
        return R.ok().put("page",page);
    }

    /**
     * 根据id获取商品分类
     */
    @ApiOperation("根据id获取商品分类")
    @GetMapping("/{id}")
    @RequiresPermissions("pms:pmsproductcategory:id")
    public R getItem(@PathVariable Long id) {
        PmsProductCategoryEntity pmsProductCategoryEntity = pmsProductCategoryService.getById(id);
        return R.ok().put("data",pmsProductCategoryEntity);
    }

    /**
     * 添加商品分类
     */
    @ApiOperation("添加商品分类")
    @PostMapping("/create")
    @RequiresPermissions("pms:pmsproductcategory:create")
    public R createProductCategory(@RequestBody PmsProductCategoryDTO pmsProductCategoryDTO){
        boolean success = pmsProductCategoryService.create(pmsProductCategoryDTO);
        if (success) {return R.ok();} else {return R.error();}
    }

    /**
     * 修改商品分类
     */
    @ApiOperation("修改商品分类")
    @PostMapping("/update/{id}")
    @RequiresPermissions("pms:pmsproductcategory:update")
    public R update(@PathVariable Long id,
                    @RequestBody PmsProductCategoryDTO pmsProductCategoryDTO){
        boolean success = pmsProductCategoryService.update(pmsProductCategoryDTO);
        if (success) {return R.ok();} else {return R.error();}
    }

    /**
     * 修改导航栏状态
     */
    @ApiOperation("修改导航栏状态")
    @PostMapping("/update/navStatus")
    @RequiresPermissions("pms:pmsproductcategory:update:navStatus")
    public R updateNavStatus(@RequestBody PmsProductCategoryEntity pmsProductCategoryEntity){
        Long id = pmsProductCategoryEntity.getId();
        Integer navStatus = pmsProductCategoryEntity.getNavStatus();
        boolean success = pmsProductCategoryService.updateNavStatusById(id,navStatus);
        if (success) {return R.ok();} else {return R.error();}
    }

    /**
     * 修改显示状态
     */
    @ApiOperation("修改显示状态")
    @PostMapping("/update/showStatus")
    @RequiresPermissions("pms:pmsproductcategory:update:showStatus")
    public R updateShowStatus(@RequestBody PmsProductCategoryEntity pmsProductCategoryEntity){
        Long id = pmsProductCategoryEntity.getId();
        Integer showStatus = pmsProductCategoryEntity.getShowStatus();
        boolean success = pmsProductCategoryService.updateShowStatusById(id,showStatus);
        if (success) {return R.ok();} else {return R.error();}
    }

    /**
     * 删除
     */
    @ApiOperation("删除商品分类")
    @PostMapping("/delete/{id}")
    @RequiresPermissions("pms:pmsproductcategory:delete")
    public R delete(@PathVariable Long id){
        boolean success = pmsProductCategoryService.removeById(id);
        if (success) {return R.ok();} else {return R.error();}
    }

    /**
     * 获取商品一级分类和二级分类下拉列表数据
     */
    @ApiOperation("获取商品一级分类和二级分类下拉列表数据")
    @GetMapping("/list/withChildren")
    @RequiresPermissions("pms:pmsproductcategory:list:withChildren")
    public R getWithChildren(){
        List<ProductWithChildrenDTO> list = pmsProductCategoryService.getWithChildren();
        return R.ok().put("data",list);
    }

}
